﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using Bll;
using System.Data;

namespace 财会系.Email
{
    public partial class EmailShow : System.Web.UI.Page
    {
        BllEmail b = new BllEmail();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if(Session["Type"]!=null)
                    datashow();
            }
        }

        public void datashow()
        {
            DataTable dt = b.Email_Inquire_All();
            GVdatashow.DataSource = dt;
            GVdatashow.DataBind();
        }
        protected void lbtnSelectAll_Click(object sender, EventArgs e)
        {
            //全选
            for (var i = 0; i < GVdatashow.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)this.GVdatashow.Rows[i].FindControl("chkSelect");
                chkSelect.Checked = true;
            }
        }
        protected void lbtnReverse_Click(object sender, EventArgs e)
        {
            //反选
            for (var i = 0; i < GVdatashow.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)this.GVdatashow.Rows[i].FindControl("chkSelect");
                chkSelect.Checked = !chkSelect.Checked;
            }
        }
        protected void lbtnCancel_Click(object sender, EventArgs e)
        {
            //取消
            for (var i = 0; i < GVdatashow.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)this.GVdatashow.Rows[i].FindControl("chkSelect");
                chkSelect.Checked = false;
            }
        }
        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            int f = 0;
            int num = 0;
            ModelPublic m = new ModelPublic();
            for (var i = 0; i < GVdatashow.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)this.GVdatashow.Rows[i].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    num++;
                    m.Id = ((Label)GVdatashow.Rows[i].FindControl("lblNewsId")).Text;               

                    bool del = b.Email_Delete(m);
                    if (del)
                    {
                        f ++;
                    }
                }
            }
            if (f>0)
            {
                Response.Write("<script>alert('删除成功！\\n您，共选中" + num + "条记录\\n共删除" + f + "条记录')</script>");
            }
            else
            {
                Response.Write("<script>alert('删除失败！')</script>");
            }

            if (search.Text == "Search...姓名、标题、类型、状态")
            {
                datashow();
            }
            else
            {
                Retrieve();
            }
        }

        protected void GVdatashow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GVdatashow.PageIndex = e.NewPageIndex;
            if (search.Text == "Search...姓名、标题、类型、状态")
            {
                datashow();
            }
            else
            {
                Retrieve();
            }
        }
        protected void sumbit_Click(object sender, ImageClickEventArgs e)
        {
            Retrieve();
        }
        protected void Retrieve()
        {
            ModelPublic m = new ModelPublic();
            if (search.Text == "Search...姓名、标题、类型、状态")
                m.Conditions = "";
            else
                m.Conditions = search.Text.Trim();

            GVdatashow.DataSource = b.Email_Search_All(m);
            GVdatashow.DataBind();
        }

        protected void GVdatashow_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#777777'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
            }
        }
    }
}